package com.example.sofclick;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class GroupListActivity extends ListActivity {
	ArrayList<String> items = new ArrayList<String>(){ {add("Ingles 6.1"); add("Ingles 6.2");} };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.grouplist);
		updateList();		
	}
	
	public void onListItemClick(ListView parent, View v, int position, long id) {
		String namegroup=items.get(position);
		Intent i=new Intent(getApplicationContext(), TabGroupActivity.class);
		startActivity(i);
	}
	
	// Initiating Menu XML file (grouplist.xml)
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menugrouplist, menu);
		return true;
	}
	
    /**
     * Event Handling for Individual menu item selected
     * Identify single menu item by it's id
     * */
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
 
        switch (item.getItemId()) {
 
        case R.id.menu_new:
            // Single menu item is selected do something
            // Ex: launching new activity/screen or show alert message
        	Intent i = new Intent(getApplicationContext(), NewGroupActivity.class);
        	startActivityForResult(i,100);
            //Toast.makeText(GroupListActivity.this, "New is Selected", Toast.LENGTH_SHORT).show();
            return true;
 
        /*case R.id.menu_delete:
            Toast.makeText(GroupListActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
            return true;*/
 
        default:
            return super.onOptionsItemSelected(item);
        }
    } 

 // Function to read the result from newly created activity
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 100) {
			if(resultCode == RESULT_OK){
				String namegroup=data.getStringExtra("result");
				items.add(namegroup);
				updateList();
			}			
		}

	}
	
	public void updateList(){
		setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, items));
	}
}
